import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const routes = [
  {
    name: 'home',
    path: '/home',
    component: () => import('pages/home'),
    children: [
      {
        name: 'home-product',
        path: 'product/:id',
        component: () => import('pages/product')
      }
    ]
  },
  {
    name: 'category',
    path: '/category',
    component: () => import('pages/category')
  },
  {
    name: 'cart',
    path: '/cart',
    component: () => import('pages/cart')
  },
  {
    name: 'personal',
    path: '/personal',
    component: () => import('pages/personal')
  },
  {
    name: 'search',
    path: '/search',
    component: () => import('pages/search')
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('pages/login')
  },
  {
    path: '*',
    redirect: '/home'
  }
]
// 添加路由守卫

const router = new Router({
  routes
})
// 添加路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.eleToken
  if (to.path !== '/cart' && to.path !== '/personal') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})
export default router
